
namespace Behemoth.SharePoint.WebParts {

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Text;
    using System.Web.UI;

  public class ErrorBag {

    private ArrayList _errors = new ArrayList();
    private ArrayList _warn = new ArrayList();
    private ArrayList _info = new ArrayList();

    public bool HasErrors {
      get {
        return (_errors.Count > 0);
      }
    }
    public bool HasWarnings {
      get {
        return (_warn.Count > 0);
      }
    }
    public bool HasInfo {
      get {
        return (_info.Count > 0);
      }
    }

    public void AddError(Exception ex) {
      _errors.Add(ex);
    }
    public void AddError(string errMsg) {
      _errors.Add(errMsg);
    }

    public void AddInfo(string msg) {
      _info.Add(msg);
    }
    public void AddWarning(string msg) {
      _warn.Add(msg);
    }

    // TODO how about some fancy/pretty rendering logic and stuff here

    private string GetIconHtml(TraceLevel level) {
      string html = "<img src=\"/_layouts/{0}\" alt=\"{1}\" />";
      switch (level) {
        case TraceLevel.Info:
          return string.Format(html, "1033/images/FilterInfo.gif", "Information"); 
        case TraceLevel.Error:
          return string.Format(html, "images/error16by16.gif", "Error");
        case TraceLevel.Warning:
          return string.Format(html, "images/FilterWarning.gif", "Warning");
      }
      return string.Empty;
    }
    private string GetSingleItemHtml(string webPartId, int number, string heading, string detailMsg) {
      StringBuilder sb = new StringBuilder();
      sb.Append("<div>");
      if (!string.IsNullOrEmpty(detailMsg)) {
        sb.Append("<img id=\"");
        sb.Append(webPartId);
        sb.Append("errBag_msg_expandImg" + number + "\" src=\"/_layouts/images/plus.gif\" alt=\"Expand\" onClick=\"MessagePanel_ToggleHelpText('");
        sb.Append(webPartId);
        sb.Append("errBag_msg', '" + number + "')\" style=\"cursor:hand;\" /> ");
      }
      sb.Append(heading);
      if (!string.IsNullOrEmpty(detailMsg)) {
        sb.Append("<div id=\"");
        sb.Append(webPartId);
        sb.Append("errBag_msg_longSpan" + number + "\" style=\"display:none;\">");
        sb.Append(detailMsg);
        sb.Append("</div>");
      }
      sb.Append("</div>");
      return sb.ToString();
    }

    private void WriteRow(HtmlTextWriter writer, TraceLevel level, string webPartId, int number, string heading, string detailMsg) {
      writer.Write("<tr><td valign=\"top\" style=\"padding-left:4px;padding-right:4px;\">");
      writer.Write(GetIconHtml(level));
      writer.Write("</td><td width=\"100%\" style=\"padding-left:4px;padding-right:4px;\">");
      writer.Write(GetSingleItemHtml(webPartId, number, heading, detailMsg));
      writer.Write("</td></tr>");
    }

    public void Render(HtmlTextWriter writer) {
      int counter = 0;
      if (this.HasErrors || this.HasWarnings || this.HasInfo) {
        writer.Write("<table class=\"ms-WPBody\" style=\"padding:0px;width:100%;\">");
        //writer.Write("<br>");
        //writer.Write("<table border='0' cellpadding='3' cellspacing='2'>");
        foreach (object err in _errors) {
          string msg = string.Empty;
          string msg2 = string.Empty;
          if (err.GetType() == typeof(string)) {
            msg = (string)err;
            msg2 = string.Empty;
          } else { // assume it is an Exception
            Exception ex = (Exception)err;
            msg =  ex.Message;
            msg2 = ex.StackTrace;
            // TODO: show inner exceptions too....
          }
          WriteRow(writer, TraceLevel.Error, "TEST_ID", counter++, msg, msg2);
          //writer.Write("  <tr><td style=\"color: red\">" + msg + "</td></tr>");
        } // foreach
        foreach (object msgo in _warn) {
          string msg = string.Empty;
          if (msgo.GetType() == typeof(string)) {
            msg = (string)msgo;
          }
          WriteRow(writer, TraceLevel.Warning, "TEST_ID", counter++, msg, string.Empty);
          //writer.Write("  <tr><td style=\"background-color: yellow; color: black\">" + msg + "</td></tr>");
        }
        foreach (object msgo in _info) {
          string msg = string.Empty;
          if (msgo.GetType() == typeof(string)) {
            msg = (string)msgo;
          }
          WriteRow(writer, TraceLevel.Info, "TEST_ID", counter++, msg, string.Empty);
          //writer.Write("  <tr><td style=\"color: black\">" + msg + "</td></tr>");
        }
        writer.Write("</table>");
      } // if HasErrors
    }

  } // class

} // namespace
